# coding=utf-8


"""武汉市 菜鸟驿站的 地址与联系方式的爬取"""


import json
import re
import time

import requests
import xlwt

from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


class NoviceStation(object):
    """菜鸟驿站"""
    def __init__(self):
        self.url = 'https://delivery.taobao.com/station/getMoreStation.do'

        self.workbook = xlwt.Workbook(encoding="utf-8", style_compression=0)  # 创建workbook对象
        self.sheet_1 = self.workbook.add_sheet("武汉菜鸟驿站信息", cell_overwrite_ok=0)
        self.hang = 1  # Excel表格行数

        self.header = {
            "method": "POST",
            "authority": "delivery.taobao.com",
            "scheme": "https",
            "path": "/station/getMoreStation.do",
            "accept": "application/json, text/javascript, */*; q=0.01",
            "origin": "https://delivery.taobao.com",
            "x-requested-with": "XMLHttpRequest",
            "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "referer": "https://delivery.taobao.com/station/showStationInPopup.htm",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            # cookie 这里是为了方便直接 写在了里面  淘宝cookie即可
            "cookie": "淘宝cookie"

        }

    def send(self, data):
        """发送"""
        for i in range(3):
            try:
                rep = requests.post(self.url, data=data, headers=self.header, verify=False, timeout=20)  # proxies=proxies,
                html = rep.content.decode('gbk')

                return html
            except Exception as e:
                print(e)
                pass

    def set_from(self):
        """组织from表单"""
        title_list = ['区域', '店铺名称', '地址', '电话']

        for n in range(len(title_list)):  # 表格
            self.sheet_1.write(0, n, title_list[n])

        areaId_list = [420102, 420103, 420104, 420105, 420106, 420107, 420111, 420112, 420113, 420114, 420115, 420116, 420117]  # 武汉市 区 编号
        area_list = ['江岸区 ', '江汉区 ', '桥口区 ', '汉阳区 ', '武昌区 ', '青山区 ', '洪山区', '东西湖区', '汉南区', '蔡甸区', '江夏区', '黄陂区', '新洲区']

        for areaid in areaId_list:
            page = 1
            while 1:
                data = {
                    '_tb_token_':	re.findall(r'_tb_token_=(.*?);', self.header['cookie'])[0],
                    'areaId': areaid,  # 区编号
                    'cityId': 420100,  # 市编号
                    'provinceId': 420000,  # 省编号
                    'lgBuyAddId': '3397864639',  # 不变
                    'addressId': '8507139837',  # 不变
                    'address': '{areaid}%5E%5E%5E%E6%B9%96%E5%8C%97%E7%9C%81%5E%5E%5E%E6%AD%A6%E6%B1%89%E5%B8%82%5E'
                               '%5E%5E%E6%B4%AA%E5%B1%B1%E5%8C%BA%5E%5E%5E%E6%B4%AA%E5%B1%B1%E8%A1%97%E9%81%93%20027'
                               '%E7%A4%BE%E5%8C%BA%5E%5E%5E%20%5E%5E%5E%E6%B4%AA%E5%B1%B1%E8%A1%97%E9%81%93%5E%5E'
                               '%5E420111007%5E%5E%5E30.521149%5E%5E%5E114.315759'.format(areaid=areaid),
                    'addrName': '%E8%82%96%E4%B8%BD%E5%A8%9C',  # 账号编码
                    'addrMobi': '15871487134',  # 电话
                    'serviceType':	101,  # 不变
                    'sellerId':	0,  # 不变
                    'userLng':	114.40038,  # 不变
                    'userLat':	30.50125,  # 不变
                    'appName':	'taobao',  # 不变
                    'page':	page,  # 页数  每页 十条
                    'stationId':	373018,  # 不变
                    'firstReq':	'false',  # 不变'
                }

                rep = self.send(data)

                if rep == '\r\n{"success":false,"errorMsg":"No stations by query params"}':
                    break

                rep_data = {
                    'area': area_list[areaId_list.index(areaid)]  # 区域
                }

                self.deal_with(rep, rep_data)

                page += 1

                time.sleep(3)

    def deal_with(self, rep, rep_data):
        """
        数据处理
        :rep : 请求返回数据
        :rep_data ：携带数据
        """

        rep_json = json.loads(rep)

        for d in rep_json['result']:
            rep_data['name'] = d['stationName']  # 店铺名称
            rep_data['address'] = re.findall(r'地址：(.*?)<br>', d['stationAddress'])[0]  # 地址
            rep_data['phone'] = re.findall(r'电话:(\d+)', d['stationAddress'])[0]  # 电话

            self.save(rep_data)

    def save(self, data):
        """保存"""
        index_dict = {'area': 0, 'name': 1, 'address': 2, 'phone': 3}
        for i in data:
            self.sheet_1.write(self.hang, index_dict[i], data[i])

        self.hang += 1
        self.workbook.save('./武汉菜鸟驿站信息.xls')


if __name__ == '__main__':
    a = NoviceStation()
    a.set_from()
